#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "libradarssd.h"

unsigned long elapsed(struct timeval* begin, struct timeval* end){
    long seconds = end->tv_sec - begin->tv_sec;
    long microseconds = end->tv_usec - begin->tv_usec;
    return seconds*1e6 + microseconds;
}

int main(int argc, char *argv[]){
    if(argc <= 1){
        printf("argment error, model path not provided!\n");
        return 0;
    }

    char* path = argv[1];
    printf("Model path:%s\n",path);

    FILE* fp;
    fp = fopen(path, "r");

    if (fp != NULL)
    {
        struct timeval begin, end;
        gettimeofday(&begin, 0);

        unsigned write_model = 0;
        float parameter = 0;
        float parameter_list[MODEL_COUNT] = {0};
        int index=0;
        while(!feof(fp)){ //读文件
            fscanf(fp,"%f\n", &parameter_list[index++]);
            // if(index<10)
            //     printf("read:%f\n",parameter_list[index-1]);
            write_model++;
        }

        update_model(parameter_list, MODEL_SIZE, 0);

        gettimeofday(&end, 0);
        fclose(fp);
        printf("total elapsed %lu us\n",elapsed(&begin, &end));

        float MB = write_model*sizeof(wtype) / (float)(1024*1024);
        float bandwith = MB/((double)elapsed(&begin, &end)/1000000);
        printf("Write %u parameters in %.2f MB\nBandwith:%.2f MB/s\n",write_model,MB,bandwith);
    }
    else
        printf("fail to open! \n");

    return 0;
}
